
#include "mainwindow.h"
#include "httpclient.h"
#include "request.h"
#include "ui_mainwindow.h"

#include <QDropEvent>
#include <QDragEnterEvent>
#include <QMimeData>
#include <QClipboard>
#include <qutils.h>

#include <widgets/picimage/picimage.h>

MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent)
  , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    setAcceptDrops(true);

    HttpClient::instance().setAutoUserAgent(true);
}

MainWindow::~MainWindow()
{
	
}

void MainWindow::dragEnterEvent(QDragEnterEvent *event)
{
    const QMimeData *localMimeData = event->mimeData();
    if (localMimeData->hasUrls()) {
        if (localMimeData->urls().at(0).isLocalFile()) {
            return event->accept();
        }
    }
}

void MainWindow::dropEvent(QDropEvent *event)
{
    QString localFile = event->mimeData()->urls()[0].toLocalFile();;
    QPixmap pixmap(localFile);
    if (pixmap.isNull()) {
        return;
    }

    QClipboard *clip = qApp->clipboard();
    clip->setPixmap(pixmap);

    ui->statusbar->showMessage("已转换完成", 2000);
}

void MainWindow::on_pushButton_clicked()
{
    Request req("https://whatslink.info/api/v1/link");
    req.addQuery("url", ui->lineEdit->text().trimmed());
    req.setHeader("Accept", "application/json, text/plain, */*");
    req.setHeader("Accept-Language", "zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6");
    req.setHeader("Connection", "keep-alive");
    req.setHeader("Host", "whatslink.info");
    req.setHeader("Origin", "https://tmp.nulla.top");
    req.setHeader("Referer", "https://tmp.nulla.top/");

    Response resp = HttpClient::instance().get(req);
    if (!resp.ok()) {
        ui->statusbar->showMessage("请求失败，请重试：再次搜索");
        return;
    }
    // Response resp(0, QString("{\"error\":\"\",\"type\":\"FOLDER\",\"file_type\":\"folder\",\"name\":\"MUKC-083\",\"size\":7295393930,\"count\":1,\"screenshots\":[{\"time\":0,\"screenshot\":\"https://whatslink.info/image/5dbd581afa3ceb8a8a1e0d8c09e83529\"},{\"time\":0,\"screenshot\":\"https://whatslink.info/image/c6397df764839ffcfac8b2209946e745\"},{\"time\":0,\"screenshot\":\"https://whatslink.info/image/cfa97e69f79e2633c88aee1b6e01c2de\"},{\"time\":0,\"screenshot\":\"https://whatslink.info/image/4446b07adcdd9b0a241d0dd33a7459ca\"},{\"time\":0,\"screenshot\":\"https://whatslink.info/image/96f3982a8290125f14ed2bf7d2219de7\"}]}"));
    qDebug() << resp.content();
    auto map = resp.toMap();
    foreach (const QString &key, map.keys()) {
        auto child = ui->groupBox->findChild<QLineEdit*>("lineedit_" + key);
        if (child) {
            QLineEdit *lineedit = qobject_cast<QLineEdit*>(child);

            if (key == "size" || key == "count") {
                lineedit->setText(QString::number(map.value(key).toLongLong()));
                continue;
            }

            lineedit->setText(map.value(key).toString());
        }
    }
    // 移除该控件下的所有元素
    {
        QUtils::qwidget_delete_childrens(ui->groupBox_screenshots);
    }

    auto screenshots = map.value("screenshots");
    if (!screenshots.isNull()) {
        count = screenshots.toList().size();
        counted = 0;
        foreach (auto var, screenshots.toList()) {
            auto time = var.toMap().value("time").toInt();
            auto screenshot = var.toMap().value("screenshot").toString();

            auto image = new PicImage();
            image->setImagePath(screenshot);
            connect(image, &PicImage::picImageRemoteSignal, this, [this](bool success){
                if (success)
                    counted++;
                ui->statusbar->showMessage(QString("总共 %1 张，已加载 %2 张。").arg(count).arg(counted));
            });
            ui->groupBox_screenshots->layout()->addWidget(image);
        }
    } else {
        qDebug() << "screenshots is null";
        ui->statusbar->showMessage(QString("没有任何图片"));
    }
}

